package com.findbus.activity;

import com.findbus.adapter.ImagePagerAdapter;
import com.findbus.project.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.util.tool.SharedPreferencesHelp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class WelcomActivity extends FragmentActivity {
	@ViewInject(R.id.activity_welcome_pager)
	private ViewPager welcome_pager;
	private ImageView image[];

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_welcome);
		initView();
	}

	private void initData() {
		// TODO Auto-generated method stub
		SharedPreferencesHelp.addLoginInfo("isFirst", true);
	}

	@SuppressLint("Recycle")
	private void initView() {
		// TODO Auto-generated method stub
		ViewUtils.inject(this);
		TypedArray type = getResources().obtainTypedArray(R.array.welecom_images);
		int length = type.length();
		image = new ImageView[length];
		for (int i = 0; i < length; i++) {
			image[i] = new ImageView(this);
			image[i].setImageResource(type.getResourceId(i, R.drawable.find1));
			image[i].setScaleType(ScaleType.FIT_XY);
			if (i == length - 1) {
				image[i].setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						starToMian();
					}
				});
			}
		}
		welcome_pager.setAdapter(new ImagePagerAdapter(image));
		welcome_pager.setOnPageChangeListener(new OnPageChangeListener() {
			boolean isLast;

			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				isLast = arg0 == welcome_pager.getCurrentItem() - 1;
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				if (isLast && arg0 == ViewPager.SCROLL_STATE_DRAGGING)
					starToMian();
			}
		});
	}

	private void starToMian() {
		startActivity(new Intent(this, LoginActivity.class));
		initData();
		finish();
	}
}
